package dao;

import model.Comment;
import util.JdbcUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CommentDao {

    /**
     * 插入新评论
     */
    public boolean insertComment(String content, int articleId, int userId) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = JdbcUtils.getConnection();
            String sql = "INSERT INTO comments (content, article_id, user_id, created_at) VALUES (?, ?, ?, NOW())";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, content);
            stmt.setInt(2, articleId);
            stmt.setInt(3, userId);

            int rows = stmt.executeUpdate();
            return rows > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            JdbcUtils.close(conn, stmt);
        }
    }

    /**
     * 根据文章ID获取评论列表
     */
    public List<Comment> findByArticleId(int articleId, int limit, int offset) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Comment> comments = new ArrayList<>();

        try {
            conn = JdbcUtils.getConnection();
            String sql = "SELECT c.*, u.username as author_name " +
                    "FROM comments c " +
                    "JOIN users u ON c.user_id = u.id " +
                    "WHERE c.article_id = ? " +
                    "ORDER BY c.created_at DESC " +
                    "LIMIT ? OFFSET ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, articleId);
            stmt.setInt(2, limit);
            stmt.setInt(3, offset);
            rs = stmt.executeQuery();

            while (rs.next()) {
                comments.add(resultSetToComment(rs));
            }
            return comments;

        } catch (SQLException e) {
            e.printStackTrace();
            return comments;
        } finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

    /**
     * 获取文章的总评论数
     */
    public int countByArticleId(int articleId) {
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;

        try {
            conn = JdbcUtils.getConnection();
            String sql = "SELECT COUNT(*) as total FROM comments WHERE article_id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, articleId);
            rs = stmt.executeQuery();

            if (rs.next()) {
                return rs.getInt("total");
            }
            return 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return 0;
        } finally {
            JdbcUtils.close(conn, stmt, rs);
        }
    }

    /**
     * 删除评论
     */
    public boolean deleteComment(int commentId) {
        Connection conn = null;
        PreparedStatement stmt = null;

        try {
            conn = JdbcUtils.getConnection();
            String sql = "DELETE FROM comments WHERE id = ?";
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, commentId);

            int rows = stmt.executeUpdate();
            return rows > 0;

        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            JdbcUtils.close(conn, stmt);
        }
    }

    /**
     * ResultSet转Comment对象
     */
    private Comment resultSetToComment(ResultSet rs) throws SQLException {
        Comment comment = new Comment();
        comment.setId(rs.getInt("id"));
        comment.setContent(rs.getString("content"));
        comment.setArticleId(rs.getInt("article_id"));
        comment.setUserId(rs.getInt("user_id"));
        comment.setAuthorName(rs.getString("author_name"));
        comment.setCreatedAt(rs.getTimestamp("created_at"));
        return comment;
    }
}